# url:http://eid.csrc.gov.cn/1011/index.html
import time

import requests
from datetime import datetime

from fake_useragent import UserAgent
from lxml import html

from operating_sql import SQLManager


class CheckUpdate:
    def __init__(self):
        # 三表类型 利润，负载，流量
        self.table_type = ["income", "balance", "cash_flow"]
        self.headers = {
            "User-Agent": UserAgent().ie
        }
        self.sql = SQLManager()
        self.xueqiu_headers = {
            'User-Agent': UserAgent().ie,
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "Host": "stock.xueqiu.com",
            "Pragma": "no-cache",
            "Sec-Fetch-Mode": "navigate",
            "Sec-Fetch-Site": "none",
            "Sec-Fetch-User": "?1",
            "Upgrade-Insecure-Requests": "1",
            "Cookie": "device_id=66c9ce413d8d7cb2336815ac6a38fd0d; s=c111ybndx0; xq_a_token=3242a6863ac15761c18a8469b89065b03bd5e164; xqat=3242a6863ac15761c18a8469b89065b03bd5e164; xq_r_token=729679220e12a2fbd19b15c94e6b7624c5ea8702; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTYwNDgwMzAzMSwiY3RtIjoxNjAyMjQxNTUxOTk1LCJjaWQiOiJkOWQwbjRBWnVwIn0.A7T2psev7vxcvYgD8XjFdhkN4CYElre_cFdjhcfmGql0FTQrVCMxGQw60XCaEs0Yx09LfqByuOlSKeh2MIqWjMYvz9i7LvuqreBtMqTR1UnRZX9uolIY6ufxYAX1Ic5MCj8jvtiiMc0zfRWkLW1J471v8BJEaZNl_6qGEvNKEXUA8vWoJPz7Y5k5-7HL7u53wf19r1tr4fz4G3DhGwuiMxV0_2V9i_Xbb810gKPO6nPHOWaeuFTOB2AsLIL916fqeC4A9jSPHl5PqxYqkmPc_5P10bGZTkpWjmn0C0sdpCTyl6V_ROycu-mf9E_cXltoPqH54zHxsNri15dySAEMJw; u=111602241594487; Hm_lvt_1db88642e346389874251b5a1eded6e3={}; Hm_lpvt_1db88642e346389874251b5a1eded6e3={}".format(
                str(time.time()).split(".")[:10], str(time.time()).split(".")[:10])
        }
        self.start_time = datetime.strptime("2020-10-15", '%Y-%m-%d').date()
        self.end_time = datetime.strptime("2020-10-10", '%Y-%m-%d').date()
        self.hu_url = "http://eid.csrc.gov.cn/101111/index_{}.html"
        self.shen_url = "http://eid.csrc.gov.cn/101811/index_{}.html"
        # 链接：cn 国内数据，us：美投
        self.xueqiu_url = "https://stock.xueqiu.com/v5/stock/finance/cn/{}.json?symbol={}&type={}&is_detail=true&count=5&timestamp="
        self.hu_tatal_page = 0
        self.shen_tatal_page = 0
        self.update_list = []

    def user_input(self):
        while True:
            try:
                start_input = str(input("请输入开始的时间段(例如：2019-6-8)："))
                end_input = str(input("请输入结束的时间段(例如：2019-6-3)："))
                self.start_time = datetime.strptime(start_input, '%Y-%m-%d').date()
                self.end_time = datetime.strptime(end_input, '%Y-%m-%d').date()
                print("输入成功正在检验")
                break
            except:
                print("开始时间或者结束时间输入错误，请重新输入")

    def get_html(self, url):
        self.update_list = []
        # html_ = requests.get("http://eid.csrc.gov.cn/101111/index_1.html", headers=self.headers).text
        # parse_html = html.fromstring(html_)
        # self.hu_tatal_page = parse_html.xpath("//ul[@class='g-ul']/li[7]/b")
        for page in range(1, 50):
            html_ = requests.get(url.format(page), headers=self.headers).text
            parse_html = html.fromstring(html_)
            li_list = parse_html.xpath("//div[@id='txt']/table//tr")
            if len(li_list) < 2:
                return
            for li in li_list[1:]:
                ticker = li.xpath("./td[1]/a/text()")[0]
                company = li.xpath("./td[2]/text()")[0]
                title = li.xpath("./td[3]/a/text()")[0].strip()
                if "2020" not in title:
                    continue
                types = li.xpath("./td[5]/text()")[0].strip()
                announcement_time = datetime.strptime(li.xpath("./td[6]/text()")[0], '%Y-%m-%d').date()
                if announcement_time.__ge__(self.end_time) and announcement_time.__le__(self.start_time):
                    print(ticker, company, title, types, announcement_time)
                    if ticker not in self.update_list:
                        self.update_list.append((ticker, types))
                elif announcement_time.__lt__(self.end_time):
                    break
        print(self.update_list)

    def run(self):
        self.user_input()
        self.get_html(self.shen_url)
        self.update_data()
        self.get_html(self.hu_url)
        self.update_data()

    def update_data(self):
        for data in self.update_list:
            if data[1] == "三季报":
                types = "Q3"
            elif data[1] == "一季报":
                types = "Q1"
            elif data[1] == "中报" or data[1] == "半年报":
                types = "Q2"
            elif data[1] == "年报":
                types = "Q4"
            else:
                types = data
                print("没有找到匹配类型", types)
                # 记录错误日志
                with open("log.txt", "a", encoding="utf-8") as f:
                    f.write(",".join(types) + "\n")
                continue
            # 此处询问：网站如果更新了，是不雪球的三个表（利润，负债，流量）也是一起更新

            # 遍历三表同时更新
            for table in self.table_type:
                ticker = "SH" + data[0]
                json_ = requests.get(self.xueqiu_url.format(table, ticker, types), headers=self.xueqiu_headers).json()
                if not json_["data"]["list"]:
                    ticker = "SZ" + data[0]
                    json_ = requests.get(self.xueqiu_url.format(table, ticker, types),
                                         headers=self.xueqiu_headers).json()
                if table == "income":
                    financials = "利润表"
                elif table == "balance":
                    financials = "负债表"
                elif table == "cash_flow":
                    financials = "资金流量表"
                for info in json_["data"]["list"]:
                    if info["report_name"] == "2020" + data[1]:
                        for i in info:
                            if i == "report_date" or i == "report_name" or i == "ctime":
                                continue
                            if info[i][0]:
                                y_0 = info[i][0]
                            else:
                                y_0 = 0
                            # 此处验证表中是否存在
                            sql_cx = 'select * from xueqiu_data where company="%s" and report_type="%s" and financials="%s" and item="%s"' % (
                                ticker, data[1], financials, i)
                            status = self.sql.get_tuple_one(sql_cx)
                            if status:
                                update_sql = 'update xueqiu_data set y_0="%s" where  company="%s" and report_type="%s" and financials="%s" and item="%s"' % (
                                    y_0, ticker, data[1], financials, i)
                                self.sql.run(update_sql)
                                print("更新语句", ticker, data[1], financials, i, y_0)
                            else:
                                ins_sql = 'insert into xueqiu_data(company,report_type,financials,item,y_0) values ("%s","%s","%s","%s","%s")' % (
                                    ticker, data[1], financials, i, y_0)
                                self.sql.run(ins_sql)
                                print("插入语句", ticker, data[1], financials, i, y_0)


if __name__ == '__main__':
    cu = CheckUpdate()
    cu.run()
